<?php
class EdmsModel extends Db
{
    protected $_dealers = 'dbo.dealers'; //经销商信息
    protected $_sales_order = 'dbo.sales_order'; //订单列表
    protected $_sales_order_list = 'dbo.sales_order_list'; //订单详细信息
    

    /**
     * 获取经销商名称
     * Enter description here ...
     */
    public function getDealersByWHERE($name)
    {
        //	$name=iconv('UTF-8', 'GBK', '魏明');
        $sql = "SELECT * FROM $this->_dealers WHERE dealer_name='$name'";
        return $this->fetch($sql);
    
     //return $this->getOne($this->_dealers, $where);
    }
    
    public function getXs()
    {
        $sql = " SELECT 
sales_order.sale_id,   
         sales_order.inde_date,   
         sales_order.dealer_id,   
         sales_order.sale_amount_sum,   
         sales_order.remarks,   	
         sales_order.op_name,   
case when (sales_order.sale_qty_sum<>0 and sales_order.sale_qty_sum - (select SUM(deliver_qty) from view_delivery_sales_order_sum where view_delivery_sales_order_sum.sale_id=sales_order.sale_id) < = 0 )
       	  or (sales_order.sale_qty_aid_sum<>0 and sales_order.sale_qty_aid_sum - (select SUM(deliver_qty_aid) from view_delivery_sales_order_sum where view_delivery_sales_order_sum.sale_id=sales_order.sale_id) < = 0 )
 	 then 1 
else  0 end as finish_flag,  
dealers.dealer_name,
dealers.dealer_sort
  FROM sales_order   
left outer join dealers with (nolock) on dealers.dealer_id=sales_order.dealer_id
where sales_order.file_flag=0 and dealers.dealer_id in (select dealer_id from dealers_popedom where oper_num='003' )";
        return $this->fetchAll($sql);
    }
    /**
     * 增加订单
     * Enter description here ...
     * @param unknown_type $params
     */
    public function addOrder($sale_id, $dealer_id, $sale_qty_sum, $sale_amount_sum, $remark, $op_name, $inde_date, $op_time)
    {
        $sql = "insert into edms.dbo.sales_order (sale_id,dealer_id,sale_qty_sum,sale_amount_sum,remarks,op_name,inde_date,op_time) VALUES ($sale_id,'$dealer_id',$sale_qty_sum,$sale_amount_sum,'$remark','$op_name','$inde_date','$op_time')";
        return $this->exec($sql);
    
     //  $this->add($this->_sales_order, $params);
    }
    
    /**
     * 获取最新的订单号
     * Enter description here ...
     */
    public function getLastSaleId()
    {
        $sql = "select max(sale_id) id from edms.dbo.sales_order";
        return $this->fetch($sql);
    }
    
    /**
     * 
     * 添加订单详情
     * @param int $sale_id  订单号
     * @param int $prod_id  产品id
     * @param int $sale_qty  销售总数
     * @param decimal $price  产品价格
     * @param decimal $amount  销售总价
     * @param date $deliver_date  发货日期
     * @param string $remarks 备注
     */
    public function addOrderList($sale_id, $sale_no, $prod_id, $sale_qty, $price, $amount, $remarks)
    {
        $sql = "INSERT INTO edms.dbo.sales_order_list (sale_id,sale_no,prod_id,sale_qty_aid,tax_price,tax_amount,remarks) VALUES ($sale_id,$sale_no,$prod_id,$sale_qty,'$price','$amount','$remarks')";
        return $this->exec($sql);
    }
    
    /**
     * [prod_id]  产品id
      ,[prod_name]
      ,[prod_sort]
      ,[unit]
      ,[prod_drno]
      ,[remarks]
      ,[imag]
      ,[file_flag]
      ,[file_name]
      ,[file_time]
      ,[file_reas]
      ,[op_name]
      ,[op_time]
      ,[unit_aid]
      ,[modulus_aid]
      ,[prod_spec]  /一箱有多少个/
     * */
    protected $_pro_list = 'dbo.products'; //产品基本信息
    /**
     * [auid]
      ,[prod_id]   产品id
      ,[dealer_id]
      ,[sale_price]  产品价格
      ,[remarks]
      ,[op_name]
      ,[op_time]
     */
    protected $_sales_price = 'dbo.sales_price'; //产品价格
    

    public function __construct()
    {
        $config = require 'edms.php';
        parent::__construct($config); //初始化父类 这样就可以用父类的方法了
    }
    
    /**
     * 获取收货信息
     * 
     * @param unknown_type $where
     */
    public function getProductsByWHERE($prod_name)
    {
        $sql = "select top 1 * from dbo.products a left join dbo.sales_price b on a.prod_id=b.prod_id where a.prod_name like '%$prod_name%'";
        return $this->fetch($sql);
    }
    
    public function getProductsByTitle($prod_name)
    {
        $sql = "select  * from dbo.products a left join dbo.sales_price b on a.prod_id=b.prod_id where a.prod_name like '%$prod_name%'";
        return $this->fetchAll($sql);
    }
    
    /**
     * 获取产品信息
     * Enter description here ...
     * @param unknown_type $prod_name
     */
    public function getProductsByName($prod_name)
    {
        $sql = "select  * from edms.dbo.products WHERE prod_name ='$prod_name'";
        return $this->fetch($sql);
    }
    
    /**
     * 返回EdmsModel
     * @return EdmsModel
     */
    public static function instance()
    {
        return self::_instance(__CLASS__);
    }
}